package com.fhzn.auth.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fhzn.auth.entity.User;
import com.fhzn.auth.web.request.RoleQueryRequest;
import com.fhzn.auth.web.request.UserQueryRequest;

import java.util.Date;
import java.util.List;

/**
 * @author 老顾
 * @apiNote
 * @since 2023/12/22
 */
public interface UserMapper extends BaseMapper<User> {

    /**
     * 根据用户id列表查询用户
     *
     * @param userIdList 用户id
     * @param request    角色请求
     * @return 用户列表
     */
    default Page<User> pageQueryUserListByRoleId(List<Integer> userIdList, RoleQueryRequest request) {
        LambdaQueryWrapper<User> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(User::getId, userIdList);
        queryWrapper.orderByAsc(User::getId);
        return selectPage(Page.of(request.getPage(), request.getPageSize()), queryWrapper);
    }

    /**
     * 根据昵称列表查询用户
     *
     * @param userNames 用户昵称
     * @return 用户列表
     */
    default List<User> listUserByNickNames(List<String> userNames) {
        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery();
        wrapper.in(User::getNickname, userNames);
        return selectList(wrapper);
    }

    /**
     * 根据昵称查询用户
     *
     * @param nickname 用户昵称
     * @return 用户
     */
    default User findUserByNickName(String nickname) {
        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(User::getNickname, nickname);
        wrapper.eq(User::getStatus, 1);
        return selectOne(wrapper);
    }


    /**
     * 根据分页查询用户
     *
     * @param request 用户请求
     * @return 用户列表
     */
    default Page<User> query(UserQueryRequest request) {
        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery();
        if (request.getNickname() != null && !request.getNickname().isEmpty()) {
            wrapper.like(User::getNickname, request.getNickname());
        }
        if (request.getStatus() != null) {
            wrapper.eq(User::getStatus, request.getStatus());
        }
        wrapper.orderByDesc(User::getCreatedTime);
        return selectPage(Page.of(request.getPage(), request.getPageSize()), wrapper);
    }

    /**
     * 重置密码
     *
     * @param user 用户
     */
    default void resetPassword(User user) {
        UpdateWrapper<User> updateWrapper = Wrappers.update();
        updateWrapper.eq("nickname", user.getNickname()).set("updated_time", new Date());
        if (user.getSalt() != null && !user.getSalt().isEmpty()) {
            updateWrapper.set("salt", user.getSalt());
        }
        if (user.getPasswordExpired() != null) {
            updateWrapper.set("password_expired", user.getPasswordExpired());
        }
        if (user.getPassword() != null && !user.getPassword().isEmpty()) {
            updateWrapper.set("password", user.getPassword());
        }
        if (user.getModifier() != null && !user.getModifier().isEmpty()) {
            updateWrapper.set("modifier", user.getModifier());
        }
        update(null, updateWrapper);
    }

    /**
     * 更新用户信息
     *
     * @param user 用户
     */
    default void updateUserInfo(User user) {
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("nickname", user.getNickname()).set("updated_time", new Date());
        if (user.getPasswordExpired() != null) {
            updateWrapper.set("password_expired", user.getPasswordExpired());
        }
        if (user.getPassword() != null && !user.getPassword().isEmpty()) {
            updateWrapper.set("password", user.getPassword());
        }
        if (user.getModifier() != null && !user.getModifier().isEmpty()) {
            updateWrapper.set("modifier", user.getModifier());
        }
        if (user.getStatus() != null) {
            updateWrapper.set("status", user.getStatus());
        }
        update(null, updateWrapper);
    }
}
